package com.topscomm.mis.cbb.pm.service.abs;

import com.topscomm.basic.exception.ServiceException;
import com.topscomm.mis.cbb.pm.api.IProjectService;
import com.topscomm.mis.cbb.pm.api.ITaskService;
import com.topscomm.mis.cbb.pm.base.pojo.CbbPmProjectEntity;
import com.topscomm.mis.cbb.pm.base.pojo.CbbPmTaskEntity;
import com.topscomm.mis.cbb.pm.base.service.ICbbPmProjectService;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.tap.common.TapSystemConst;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.transaction.annotation.Transactional;

import javax.mail.MessagingException;
import java.util.HashMap;
import java.util.Map;

@ConditionalOnMissingBean(ITaskService.class)
public abstract class AbstractProjectService implements IProjectService {
    @Autowired
    protected ICbbPmProjectService cbbPmProjectService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> insert(Map<String, Object> paramMap) {
        beforeInsert(paramMap);
        CbbPmProjectEntity projectEntity = new CbbPmProjectEntity();
        projectEntity.convertFromMap(paramMap);
        projectEntity = cbbPmProjectService.insert(projectEntity);
        Map<String, Object> dataMap = projectEntity.convertToMap();
        afterInsert(dataMap);
        return dataMap;
    }

    protected void beforeInsert(Map<String, Object> paramMap) throws ServiceException {
    }

    protected void afterInsert(Map<String, Object> paramMap) throws ServiceException {
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Map<String, Object> paraMap) {
        beforeUpdate(paraMap);
        int state = ConvertUtil.convertToInt(paraMap.get(CbbPmProjectEntity.FieldState));
        if(state == -1) {
            try {
                throw new MessagingException("该项目已废弃，无法进行修改！");
            } catch (MessagingException e) {
                throw new RuntimeException(e);
            }
        }
        cbbPmProjectService.update(paraMap);
        afterUpdate(paraMap);
    }

    protected void beforeUpdate(Map<String, Object> paramMap) throws ServiceException {
    }

    protected void afterUpdate(Map<String, Object> paramMap) throws ServiceException {
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Map<String, Object> paraMap) {
        String ids = ConvertUtil.convertToString(paraMap.get("ids"));
        cbbPmProjectService.deleteByWhere(" id in (" + ids + ")");
    }

    @Override
    public String generateProjectUuCode() {
        return "";
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateState(Map<String, Object> paraMap) {
        Map<String, Object> updateMap = new HashMap<>();
        String uuCode = ConvertUtil.convertToString(paraMap.get("projectuucode"));
        String tableName = ConvertUtil.convertToString(paraMap.get("tableName"));
        String state = ConvertUtil.convertToString(paraMap.get("state"));
        updateMap.put(CbbPmProjectEntity.FieldState, state);
        StringBufferProxy sql = new StringBufferProxy();
        sql.appendSingle(" {0} = '{1}'",CbbPmProjectEntity.FieldCode,uuCode);
        sql.appendSingle(" and {0} = '{1}'",CbbPmProjectEntity.FieldType,tableName);
        cbbPmProjectService.updateByWhere(updateMap,sql.toString());
    }
}
